//
//  ZROriginal.m
//  SinaByOC
//
//  Created by 张锐 on 16/3/19.
//  Copyright © 2016年 zhangr. All rights reserved.
//

#import "ZROriginal.h"
#import <Masonry.h>
#import <UIImageView+WebCache.h>
#import "ZRPicture.h"

@interface ZROriginal()

@property (nonatomic,strong) UIImageView * iconImageView;

@property (nonatomic,strong) UIImageView * registerImageView;

@property (nonatomic,strong) UILabel * nameLabel;

@property (nonatomic,strong) UIImageView * vipImageView;

@property (nonatomic,strong) UILabel * timeLabel;

@property (nonatomic,strong) UILabel * sourceLabel;

@property (nonatomic,strong) UILabel * contextLabel;

@property (nonatomic,strong) ZRPicture * pic;

@end
@implementation ZROriginal

-(instancetype)init{

    self = [super init];
    
    if (self) {
        
        [self setupUI];
        
    }
    return self;

}
//添加控件设置约束
-(void)setupUI{
    
    [self addSubview:self.iconImageView];
    
    [self addSubview:self.registerImageView];
    
    [self addSubview:self.nameLabel];
    
    [self addSubview:self.vipImageView];
    
    [self addSubview:self.timeLabel];
    
    [self addSubview:self.sourceLabel];
    
    [self addSubview:self.contextLabel];
    
    [self addSubview:self.pic];
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.mas_left).offset(10);
        
        make.top.equalTo(self.mas_top).offset(10);
        
        make.size.equalTo([NSValue valueWithCGSize:CGSizeMake(40, 40)]);
        
    }];
    
    [self.registerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.iconImageView.mas_right);
        
        make.centerY.equalTo(self.iconImageView.mas_bottom);
        
        make.size.equalTo([NSValue valueWithCGSize:CGSizeMake(12, 12)]);
        
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.iconImageView.mas_right).offset(10);
        
        make.top.equalTo(self.mas_top).offset(13);
        
    }];
    
    [self.vipImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.nameLabel.mas_right).offset(10);
        
        make.centerY.equalTo(self.nameLabel.mas_centerY).offset(-2);
        
        make.size.equalTo([NSValue valueWithCGSize:CGSizeMake(14, 14)]);
        
    }];

    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.iconImageView.mas_right).offset(10);
        
        make.bottom.equalTo(self.iconImageView.mas_bottom).offset(3);
        
    }];

    [self.sourceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    
        make.left.equalTo(self.timeLabel.mas_right).offset(10);
        
        make.centerY.equalTo(self.timeLabel.mas_centerY);
    }];
    
    [self.contextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.mas_left).offset(10);
        
        make.top.equalTo(self.iconImageView.mas_bottom).offset(13);
        
        make.right.equalTo(self.mas_right).offset(-10);
        
//        make.bottom.equalTo(self.mas_bottom);
    }];
    [self.pic mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.mas_left).offset(10);
        
        make.top.equalTo(self.contextLabel.mas_bottom);
        
        make.size.equalTo([NSValue valueWithCGSize:CGSizeMake(100, 100)]);
        
        make.bottom.equalTo(self.mas_bottom);
    }];
}
//懒加载
-(UIImageView *)iconImageView{

    if (_iconImageView == nil) {
        
        _iconImageView = [[UIImageView alloc]init];
        
        _iconImageView.image = [UIImage imageNamed:@"avatar_default_big"];
        
    }
    return  _iconImageView;
}
-(UIImageView *)registerImageView{

    if (_registerImageView == nil) {
        
        _registerImageView = [[UIImageView alloc]init];
        
        _registerImageView.image = [UIImage imageNamed:@"avatar_vip"];
    }
    return _registerImageView;

}
-(UILabel *)nameLabel{

    if (_nameLabel == nil) {
        
        _nameLabel = [[UILabel alloc]init];
        
        _nameLabel.text =@"微博名称";
        
        _nameLabel.font = [UIFont systemFontOfSize:14];
    }
    return _nameLabel;
}
-(UIImageView *)vipImageView{

    if (_vipImageView == nil) {
        
        _vipImageView = [[UIImageView alloc]init];
        
        _vipImageView.image = [UIImage imageNamed:@"common_icon_membership"];
    }
    return _vipImageView;

}
-(UILabel *)timeLabel{

    if (_timeLabel == nil) {
        
        _timeLabel = [[UILabel alloc]init];
        
        _timeLabel.text = @"xx分钟前";
        
        _timeLabel.textColor = [UIColor orangeColor];
        
        _timeLabel.font = [UIFont systemFontOfSize:12];
    }
    return _timeLabel;

}
-(UILabel *)sourceLabel{

    if (_sourceLabel == nil) {
        
        _sourceLabel = [[UILabel alloc]init];
        
        _sourceLabel.text = @"火星探测仪";
        
        _sourceLabel.textColor = [UIColor darkGrayColor];
        
        _sourceLabel.font = [UIFont systemFontOfSize:12];
    }
    return _sourceLabel;

}
-(UILabel *)contextLabel{

    if (_contextLabel == nil) {
        
        _contextLabel = [[UILabel alloc]init];
        
        _contextLabel.text = @"微博内容";
        
        _contextLabel.font = [UIFont systemFontOfSize:14];
        
        _contextLabel.numberOfLines = 0;
        
    }
    return  _contextLabel;

}
-(void)setHomeCellModel:(ZRHomeCellModel *)homeCellModel{

    _homeCellModel = homeCellModel;

    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:homeCellModel.user.profile_image_url]];
    //-1 : return nil
    //0 : return UIImage(named: "avatar_vip")
    //2,3,5 : return UIImage(named: "avatar_enterprise_vip")
    //220 : return UIImage (named: "avatar_grassroot")
    
    switch (homeCellModel.user.verified_type) {
        case -1:
            self.registerImageView.hidden = YES;
            break;
        case 0:
            self.registerImageView.hidden = NO;
            self.registerImageView.image = [UIImage imageNamed:@"avatar_vip"];
            break;
        case 2:
            self.registerImageView.hidden = NO;
            self.registerImageView.image = [UIImage imageNamed:@"avatar_enterprise_vip"];
            break;
        case 3:
            self.registerImageView.hidden = NO;
            self.registerImageView.image = [UIImage imageNamed:@"avatar_enterprise_vip"];
            break;
        case 5:
            self.registerImageView.hidden = NO;
            self.registerImageView.image = [UIImage imageNamed:@"avatar_enterprise_vip"];
            break;
        case 220:
            self.registerImageView.hidden = NO;
            self.registerImageView.image = [UIImage imageNamed:@"avatar_grassroot"];
            break;
    }
    
    self.nameLabel.text = homeCellModel.user.screen_name;
    
    if (homeCellModel.user.mbrank >0 && homeCellModel.user.mbrank <=6) {
        
        self.vipImageView.hidden = NO;
        
        self.vipImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"common_icon_membership_level%d",homeCellModel.user.mbrank]];
    }else{
    
        self.vipImageView.hidden = YES;
    }
    
    self.timeLabel.text = homeCellModel.created_at;
    
    NSString * tempStr =[[homeCellModel.source componentsSeparatedByString:@"\">"] lastObject];

    NSString * lastStr = [[tempStr componentsSeparatedByString:@"<"] firstObject];

    self.sourceLabel.text = [NSString stringWithFormat:@"来自 %@",lastStr];
    
    
    self.contextLabel.text = homeCellModel.text;
}

-(ZRPicture *)pic{

    if (_pic == nil) {
        
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        
        _pic = [[ZRPicture alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        
        
    }
    return _pic;

}

@end
